package com.example.module_filemanage.Utils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.example.module_filemanage.Interface.ICallBack;

import java.io.File;
import java.text.DecimalFormat;


import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

import static android.content.ContentValues.TAG;

/**
 * Created by SJ on 2018/4/20.
 */

public class Tool {
    //byte转 gb/mb/kb
    public static String getBytetoSize(long size){
        float GB=1024.0f*1024.0f*1024.0f;
        float MB=1024.0f*1024.0f;
        float KB=1024.0f;
        String ss="";
        DecimalFormat df = new DecimalFormat("#.00");
        if (size>GB){
            ss = df.format((float)size/GB)+"G";
        }else if(size>MB){
            ss = df.format((float)size/MB)+"M";
        }else if(size>KB){
            ss = df.format((float)size/KB)+"K";
        }else{
            ss = df.format(size)+"B";
        }
        return ss;
    }
    //进度条进度
    public static int getprogress(long size){
        float GB=1024*1024*1024.0f;
        int progress= (int)((size/(10*GB))*10000);
        Log.e(TAG, "getprogress: "+String.valueOf((size/(10*GB))*10000));
        return progress;
    }
    //单文件压缩调用
    public static void CompressImage(final Context mcontext, String imagepath, final ICallBack back) {
        try {

            Luban.get(mcontext)
                    .load(new File(imagepath)) // 要压缩的图片
                    .setCompressListener(new OnCompressListener() {
                        @Override
                        public void onStart() {
                            // 压缩开始前调用 可以在方法内启动loading UI

                        }

                        @Override
                        public void onSuccess(File newFile) {
                            // 压缩成功后调用，返回压缩后的图片文件
                            Log.e(TAG, String.format("Size : %s", getBytetoSize(newFile.length())));
                            back.success(newFile.getAbsolutePath());
                            //Toast.makeText(mcontext, newFile.getAbsolutePath(), Toast.LENGTH_SHORT).show();
                            // 设置获取图片大小
                            //   ((TextView) (findViewById(R.id.id_luban_compress_end_size))).setText(String.format("Size : %s", StringUtils.getReadableFileSize(newFile.length())));
                        }

                        @Override
                        public void onError(Throwable e) {
                            // 压缩过程中出现异常
                            Toast.makeText(mcontext, "压缩出错" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }).launch(); // 启动压缩
        } catch (Exception e) {
            Toast.makeText(mcontext, "读取图片失败~", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
    /**
     * 获取可读的文件大小
     */
    private static String getReadableFileSize(long size) {
        final int BYTES_IN_KILOBYTES = 1024;
        final DecimalFormat dec = new DecimalFormat("###.#");
        final String KILOBYTES = " KB";
        final String MEGABYTES = " MB";
        final String GIGABYTES = " GB";
        float fileSize = 0;
        String suffix = KILOBYTES;
        if(size > BYTES_IN_KILOBYTES) {
            fileSize = size / BYTES_IN_KILOBYTES;
            if(fileSize > BYTES_IN_KILOBYTES) {
                fileSize = fileSize / BYTES_IN_KILOBYTES;
                if(fileSize > BYTES_IN_KILOBYTES) {
                    fileSize = fileSize / BYTES_IN_KILOBYTES;
                    suffix = GIGABYTES;
                } else {
                    suffix = MEGABYTES;
                }
            }
        }
        return String.valueOf(dec.format(fileSize) + suffix);
    }
}
